package org.jeecg.modules.villageCadreAssist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.villageCadreAssist.entity.VcaDeath;
import org.jeecg.modules.villageCadreAssist.entity.VcaFamily;
import org.jeecg.modules.villageCadreAssist.entity.VcaResident;
import org.jeecg.modules.villageCadreAssist.mapper.VcaDeathMapper;
import org.jeecg.modules.villageCadreAssist.service.IVcaDeathService;
import org.jeecg.modules.villageCadreAssist.service.IVcaFamilyService;
import org.jeecg.modules.villageCadreAssist.service.IVcaResidentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: vca_death
 * @Author: jeecg-boot
 * @Date:   2022-03-14
 * @Version: V1.0
 */
@Service
public class VcaDeathServiceImpl extends ServiceImpl<VcaDeathMapper, VcaDeath> implements IVcaDeathService {

    @Autowired
    private IVcaResidentService vcaResidentService;
    @Autowired
    private IVcaFamilyService vcaFamilyService;

    public IPage<VcaDeath> pageList(Page<VcaDeath> vcaDeathPage, QueryWrapper<VcaDeath> queryWrapper, String orgCode){
        return baseMapper.pageList(vcaDeathPage,queryWrapper,orgCode);
    }

    @Override
    @Transactional
    public void add(VcaDeath vcaDeath) {
        super.save(vcaDeath);

        VcaResident resident = vcaResidentService.getById(vcaDeath.getResidentId());
        VcaFamily family = vcaFamilyService.getOne(new LambdaQueryWrapper<VcaFamily>().eq(VcaFamily::getFamilyCode, resident.getFamilyCode()));
        VcaFamily f = new VcaFamily();
        f.setId(family.getId());
        if(family.getFamilyNum()-1 == 0){//修改家庭人数
            f.setFamilyNum(family.getFamilyNum()-1);
        }else{//销户
            f.setExtinct(1);
        }
        vcaFamilyService.updateById(f);

        VcaResident r = new VcaResident();
        r.setId(resident.getId());
        r.setDeath(1);
        vcaResidentService.updateById(r);
    }

    @Override
    @Transactional
    public void delete(String id) {

        //针对 add的反操作
        VcaDeath death = super.getById(id);
        VcaResident resident = vcaResidentService.getById(death.getResidentId());
        VcaFamily family = vcaFamilyService.getOne(new LambdaQueryWrapper<VcaFamily>().eq(VcaFamily::getFamilyCode, resident.getFamilyCode()));

        VcaResident r = new VcaResident();
        r.setId(resident.getId());
        r.setDeath(0);
        vcaResidentService.updateById(r);
        VcaFamily f = new VcaFamily();
        f.setId(family.getId());
        if(family.getExtinct()==1){//修改家庭人数
            f.setExtinct(0);
        }else{//销户
            f.setFamilyNum(family.getFamilyNum()+1);
        }
        vcaFamilyService.updateById(f);
        super.removeById(id);
    }

    @Override
    public void deleteBatch(List<String> idList) {
        for (String id : idList) {
            delete(id);//TODO 暂时采用单个删除方式
        }
    }
}
